package zj.com.activities;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.DoubleLeftAdapter;
import zj.com.adapter.DoubleRightAdapter;
import zj.com.adapter.SelectAdapter;
import zj.com.app.ZjApplication;
import zj.com.model.Org;
import zj.com.model.Project;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/21.
 *
 * 输入
 */
public class SelectDoubleListActivity extends  BaseActivity implements View.OnClickListener{

    public TextView title_name,right_tv;
    public ImageView left_iv;

    public ListView left_listView,right_listView;

    public DoubleLeftAdapter leftAdapter;
    public DoubleRightAdapter rightAdapter;

    public List<Org> leftList;
    public List<Project> rightList;

    public boolean isDobule = false;
    public Org selectOrg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_double);

        initView();
    }

    public void initView(){

        isDobule = getIntent().getBooleanExtra("isDouble",false);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);

        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("确定");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);

        if(isDobule){
            right_tv.setVisibility(View.VISIBLE);

        }


        left_listView = (ListView) findViewById(R.id.left_listView);
        right_listView = (ListView) findViewById(R.id.right_listView);
        leftList =new ArrayList<>();

        rightList = new ArrayList<>();

        leftAdapter = new DoubleLeftAdapter(leftList,this);
        left_listView.setAdapter(leftAdapter);
        rightAdapter = new DoubleRightAdapter(rightList,this);
        right_listView.setAdapter(rightAdapter);


        leftList = ZjApplication.orgList;
        if(leftList != null && leftList.size()  > 0){

            leftAdapter.setDataChange(leftList);
            leftAdapter.setSelectPosition(0);

            rightList =  ZjApplication.projectMap.get(leftList.get(0).getOrgCode());
            rightAdapter.setDataChange(rightList);
        }

        left_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 selectOrg = leftList.get(position);
                List<Project> prjList =  ZjApplication.projectMap.get(selectOrg.getOrgCode());
                if(prjList != null && prjList.size() > 0){

                    rightList = prjList;
                    rightAdapter.setDataChange(prjList);
                }else{
                    obtainProject(selectOrg);
                }

                leftAdapter.setSelectPosition(position);

            }
        });

        right_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


               if(isDobule){

               }else{
                   Project project =   rightList.get(position);
                   Intent intent  = new Intent();
                   intent.putExtra("org",selectOrg);
                   intent.putExtra("project",project);
                   setResult(RESULT_OK,intent);
                   finish();
               }


            }
        });


    }

    public ProgressDialog dialog;


    public void obtainProject(final Org org){

        dialog = DialogUtil.createProgressDialog(this,"提示","获取数据");
        NetManager.getInstance(this).GetProjectList(org.getOrgCode(), new OnResponseListener<List<Project>>() {
            @Override
            public void onComplete(boolean isOK, List<Project> result, int errorCode, String errorMsg) {

                if (isOK) {

                    if(result != null && result.size() > 0){


                        rightList = result;
                        ZjApplication.projectMap.put(org.getOrgCode(), result);
                    }else{
                        rightList.clear();
                    }
                    rightAdapter.setDataChange(rightList);

                    dialog.dismiss();

                } else {
                    Toast.makeText(SelectDoubleListActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(SelectDoubleListActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:

                break;
        }
    }
}
